﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ESMaster
{
    public partial class FormMain : Form
    {
        //zhende 

        public FormMain()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            //清空所有textbox
            foreach (var control in panelContent.Controls)
            {
                if (control is TextBox)
                {
                    ((TextBox)control).Text = "";
                }
            }
            textBoxESNum.Focus();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            EngineeringSample newES = createES();
            if (newES == null) return;

            string filename = Properties.Settings.Default.Path;
            if (string.IsNullOrWhiteSpace(filename))
            {
                MessageBox.Show("The Excel file is not found, please set the path for Excel file!", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                return;
            }
            if (ExcelHelper.Export(newES, filename))
            {
                MessageBox.Show("Successful!");
            }
        }


        private EngineeringSample createES()
        {
            EngineeringSample es = new EngineeringSample();
            if (textBoxESNum.Text.Trim() == "")
            {
                MessageBox.Show("ES Number is Empty!");
                return null;
            }
            es.ESNumber = textBoxESNum.Text;
            es.Project = textBoxProject.Text;
            es.SendingSite = textBoxSendingSite.Text;
            es.PartNumber = textBoxPartNum.Text;
            es.DrawingNum = textBoxDrawNum.Text;
            es.PartDesp = textBoxPartDesp.Text;
            es.Purpose = textBoxPurpose.Text;
            es.Originator = textBoxOriginator.Text;
            es.IssueQty = textBoxIssueQty.Text;
            es.ScrapQty = textBoxScrapQty.Text;
            es.IssueSendingSite = textBoxIssueSendSite.Text;
            es.EsSampleQty = textBoxESSampleQty.Text;
            es.QcSampleQty = textBoxQCSampleQty.Text;
            es.Comments = textBoxComments.Text;
            return es;
        }

        private void buttonSetting_Click(object sender, EventArgs e)
        {
            string filename;
            OpenFileDialog opendg = new OpenFileDialog();
            opendg.Filter = "Excel Files|*.xlsx";
            if (opendg.ShowDialog() == DialogResult.OK)
            {
                filename = opendg.FileName;
                Properties.Settings.Default.Path = filename;
                Properties.Settings.Default.Save();
            }
        }
    }
}
